﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TGSCOLOR 
    { 
       
        public TGSCOLOR()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return; 
            MV = new VGSCOLOR();
            DataContext = MV;
            if (app.User.Trim() == "ITXV")
            {
                ART = "3000";
                FARBE = "Y24%";
            }
            InitializeComponent(); 
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            
            dwData = TBMain.NewRow();
            dwData["GSCMANDANT"] = app.Mandant;
            dwData["GSCART"] = " AND GSCART LIKE '%" + ART.Trim() + "%'";
            dwData["GSCFARBE"] = " AND GSCFARBE LIKE '" + FARBE.Trim() + "%'";
            SResult = TMV.QGC09(dwData);
            MergeData(SResult, false);
            base.QueryCommand(sender, e);
        }
       
        public override void LOADBYAUF()
        {

            if (!IsSaved)
                return;
            if (ORDERNUM.Length < 33)
                return;
            if (ORDERNUM != KEY)
                return;
            dwData = TBMain.NewRow();
            dwData["GSCMANDANT"] = app.Mandant;
            dwData["GSCART"] = " AND GSCART = '" + ORDERNUM.Substring(1, 20) + "'";
            dwData["GSCVAR"] = " AND GSCART = '" + ORDERNUM.Substring(21, 3) + "'";
            dwData["GSCFARBE"] = " AND GSCFARBE = '" + ORDERNUM.Substring(24, 10) + "'";
            SResult = TMV.QGC09(dwData);
            TBTemp = TBMain.Clone();
            TBTemp.Merge(SResult.Tables[0]);
            foreach (DataRow row in TBTemp.Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }
            IsSaved = false;
        }
        public override void OnDESChg(string des)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            TMV.GSCBEZ = DES;
            base.OnDESChg(des);
        }
        public override void OnCARTChg()
        {
            if (CART.Length < 23)
                return;
            ART = CART.Substring(0, 20);
            VAR = CART.Substring(20, 3);
        }

        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            IsSaved = false;
            base.EditCommand(sender, e);
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            base.SaveCommand(sender, e);
        }
    
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                return;
            }
            filter = "GSCART='" + dwMaster["GSCART"].ToString() + "' AND GSCVAR='" + dwMaster["GSCVAR"].ToString() + "' AND GSCFARBE='"+ dwMaster["GSCFARBE"].ToString()+"'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
              
                TMV.DGSC01(row);
                row.Delete();
            }
            vwMaster.View.MoveCurrentToFirst();
            base.DeleteCommand(sender, e);
        }
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                MessageBox.Show("清先选择一个记录");
                return;
            }
            if (!IsCopy)
            {
                IsCopy = true;
                ART = dwMaster["GSCART"].ToString();
                VAR = dwMaster["GSCVAR"].ToString();
                FARBE = "";
                return;
            }
            if (VAR == "0")
            {
                MessageBox.Show("清先输入颜色数");
                return;
            }
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            dwData["GSCART"] = ART.Trim().PadRight(20);
            dwData["GSCVAR"] = VAR.Trim().PadRight(3);
            dwData["GSCFARBE"] = FARBE.Trim().PadRight(10);
            TBMain.Rows.InsertAt(dwData, 0);
            TMV.InsertGSCOLOR(dwData, app.company);
            IsCopy = false;
            ART = "";
            VAR = "";
            FARBE = "";
            vwMaster.View.MoveCurrentToFirst();
            base.CopyCommand(sender, e);
        }

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (!IsNew)
            {
                IsNew = true;
                ART = "";
                VAR = "   ";
                FARBE = "";
                return;
            } 
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["GSCART"] = ART.Trim().PadRight(20);
            dwData["GSCVAR"] = VAR.Trim().PadRight(3);
            dwData["GSCFARBE"] = FARBE.Trim().PadRight(10);
            TBMain.Rows.InsertAt(dwData, 0);
            TMV.InsertGSCOLOR(dwData, app.company);
            IsNew = false; 
            VAR = "";
            FARBE = "";
            vwMaster.View.MoveCurrentToFirst();
            base.NewCommand(sender, e);
        }
    }
}
